package com.work.yshop.util;

import com.work.yshop.model.Article;

/**
 * 文章HTML生成器
 * 根据文章内容动态生成HTML页面
 */
public class ArticleHtmlGenerator {

    /**
     * 生成文章详情页面的HTML
     * @param article 文章对象
     * @return HTML字符串
     */
    public static String generateArticleHtml(Article article) {
        if (article == null) {
            return generateErrorHtml("文章不存在");
        }

        StringBuilder html = new StringBuilder();
        html.append("<!DOCTYPE html>");
        html.append("<html lang=\"zh-CN\">");
        html.append("<head>");
        html.append("<meta charset=\"UTF-8\">");
        html.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">");
        html.append("<title>").append(article.getTitle()).append("</title>");
        html.append("<style>");
        html.append("* { margin: 0; padding: 0; box-sizing: border-box; }");
        html.append("body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; color: #333; padding: 0; background-color: #fff; }");
        html.append(".container { padding: 0; max-width: 100%; }");
        html.append(".article-header { padding: 0 0 15px 0; }");
        html.append(".article-meta { display: flex; justify-content: space-between; color: #888; font-size: 14px; margin-top: 12px; }");
        html.append(".article-image { width: 100%; border-radius: 4px; margin-bottom: 16px; }");
        html.append(".article-content { font-size: 16px; line-height: 1.8; color: #333; }");
        html.append(".article-content p { margin-bottom: 16px; }");
        html.append(".article-content img { max-width: 100%; height: auto; margin: 16px 0; border-radius: 4px; }");
        html.append(".article-content h2 { font-size: 20px; margin: 24px 0 16px; color: #333; }");
        html.append(".article-content h3 { font-size: 18px; margin: 20px 0 14px; color: #333; }");
        html.append(".article-content ul, .article-content ol { margin: 16px 0; padding-left: 24px; }");
        html.append(".article-content li { margin-bottom: 8px; }");
        html.append(".article-content blockquote { border-left: 4px solid #4CAF50; padding-left: 16px; margin: 16px 0; color: #666; }");
        html.append(".article-footer { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; color: #888; font-size: 14px; text-align: center; }");
        html.append("</style>");
        html.append("</head>");
        html.append("<body>");
        html.append("<div class=\"container\">");
        
        // 文章头部
        html.append("<div class=\"article-header\">");
        if (article.getImageUrl() != null && !article.getImageUrl().isEmpty()) {
            html.append("<img class=\"article-image\" src=\"").append(article.getImageUrl()).append("\" alt=\"").append(article.getTitle()).append("\">");
        }
        html.append("<h1>").append(article.getTitle()).append("</h1>");
        html.append("<div class=\"article-meta\">");
        html.append("<span>").append(article.getAuthor() != null ? article.getAuthor() : "未知作者").append("</span>");
        html.append("<span>").append(article.getPublishDate()).append("</span>");
        html.append("<span>阅读: ").append(article.getViewCount()).append("</span>");
        html.append("</div>");
        html.append("</div>");
        
        // 文章内容
        html.append("<div class=\"article-content\">");
        // 处理内容，将换行符转换为HTML段落
        if (article.getContent() != null && !article.getContent().isEmpty()) {
            String[] paragraphs = article.getContent().split("\n\n");
            for (String paragraph : paragraphs) {
                if (!paragraph.trim().isEmpty()) {
                    // 检查是否是列表项
                    if (paragraph.trim().startsWith("- ") || paragraph.trim().startsWith("* ")) {
                        String[] listItems = paragraph.split("\n");
                        html.append("<ul>");
                        for (String item : listItems) {
                            String trimmedItem = item.trim();
                            if (trimmedItem.startsWith("- ") || trimmedItem.startsWith("* ")) {
                                html.append("<li>").append(trimmedItem.substring(2)).append("</li>");
                            }
                        }
                        html.append("</ul>");
                    }
                    // 检查是否是数字列表
                    else if (paragraph.trim().matches("^\\d+\\..*")) {
                        String[] listItems = paragraph.split("\n");
                        html.append("<ol>");
                        for (String item : listItems) {
                            String trimmedItem = item.trim();
                            if (trimmedItem.matches("^\\d+\\..*")) {
                                html.append("<li>").append(trimmedItem.substring(trimmedItem.indexOf(".") + 1).trim()).append("</li>");
                            }
                        }
                        html.append("</ol>");
                    }
                    // 普通段落
                    else {
                        html.append("<p>").append(paragraph).append("</p>");
                    }
                }
            }
        } else {
            html.append("<p>暂无内容</p>");
        }
        html.append("</div>");
        
        // 文章底部
        html.append("<div class=\"article-footer\">");
        html.append("<p>支农宝 - 农技学堂</p>");
        html.append("</div>");
        
        html.append("</div>");
        html.append("</body>");
        html.append("</html>");
        
        return html.toString();
    }
    
    /**
     * 生成错误页面的HTML
     * @param errorMessage 错误信息
     * @return HTML字符串
     */
    public static String generateErrorHtml(String errorMessage) {
        StringBuilder html = new StringBuilder();
        html.append("<!DOCTYPE html>");
        html.append("<html lang=\"zh-CN\">");
        html.append("<head>");
        html.append("<meta charset=\"UTF-8\">");
        html.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">");
        html.append("<title>错误</title>");
        html.append("<style>");
        html.append("* { margin: 0; padding: 0; box-sizing: border-box; }");
        html.append("body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; color: #333; padding: 16px; background-color: #f8f8f8; display: flex; justify-content: center; align-items: center; min-height: 100vh; }");
        html.append(".error-container { text-align: center; padding: 32px; background-color: #fff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); max-width: 90%; }");
        html.append(".error-icon { font-size: 48px; color: #ff5252; margin-bottom: 16px; }");
        html.append(".error-title { font-size: 24px; color: #333; margin-bottom: 16px; }");
        html.append(".error-message { font-size: 16px; color: #666; margin-bottom: 24px; }");
        html.append(".error-button { display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px; font-weight: 500; }");
        html.append("</style>");
        html.append("</head>");
        html.append("<body>");
        html.append("<div class=\"error-container\">");
        html.append("<div class=\"error-icon\">⚠️</div>");
        html.append("<h1 class=\"error-title\">出错了</h1>");
        html.append("<p class=\"error-message\">").append(errorMessage).append("</p>");
        html.append("<a href=\"javascript:history.back()\" class=\"error-button\">返回</a>");
        html.append("</div>");
        html.append("</body>");
        html.append("</html>");
        
        return html.toString();
    }
    
    /**
     * 生成加载中页面的HTML
     * @return HTML字符串
     */
    public static String generateLoadingHtml() {
        StringBuilder html = new StringBuilder();
        html.append("<!DOCTYPE html>");
        html.append("<html lang=\"zh-CN\">");
        html.append("<head>");
        html.append("<meta charset=\"UTF-8\">");
        html.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">");
        html.append("<title>加载中</title>");
        html.append("<style>");
        html.append("* { margin: 0; padding: 0; box-sizing: border-box; }");
        html.append("body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; color: #333; padding: 16px; background-color: #f8f8f8; display: flex; justify-content: center; align-items: center; min-height: 100vh; }");
        html.append(".loading-container { text-align: center; padding: 32px; }");
        html.append(".loading-spinner { border: 4px solid rgba(0, 0, 0, 0.1); width: 36px; height: 36px; border-radius: 50%; border-left-color: #4CAF50; animation: spin 1s linear infinite; margin: 0 auto 16px; }");
        html.append(".loading-text { font-size: 18px; color: #666; }");
        html.append("@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }");
        html.append("</style>");
        html.append("</head>");
        html.append("<body>");
        html.append("<div class=\"loading-container\">");
        html.append("<div class=\"loading-spinner\"></div>");
        html.append("<p class=\"loading-text\">加载中...</p>");
        html.append("</div>");
        html.append("</body>");
        html.append("</html>");
        
        return html.toString();
    }
} 